import axios from 'axios'
import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { setStore, getStore, removeStore } from '@/utils/store'

const user = {
    state: {
        //yangxu要存一个全局变量
        
        token: getToken(),
        name: '',
        avatar: '',
        roles: [],
        browserHeaderTitle: getStore({
            name: 'browserHeaderTitle'
        }) || '教学实验系统'
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_BROWSERHEADERTITLE: (state, action) => {
            state.browserHeaderTitle = action.browserHeaderTitle
        },
        
    },

    actions: {
       // 登录
        // Login({commit}, userInfo) {
        //     return new Promise((resolve, reject) => {
        //         axios.post('/tes/unauthorized/form', userInfo, {
        //             headers: {
        //                 'Content-Type': 'multipart/form-data'
        //             }
        //         }).then((response) => {
        //             let res = response.data
        //             if (res.code == 200) {
        //                 let roleCode = res.data.roleCode
        //                 let roles = []
        //                 roles.push(roleCode)
        //                 setToken(roleCode)
        //                 commit('SET_ROLES', roles)
        //                 resolve()
        //             }
        //         }).catch(err => {
        //             console.log('服务器错误')
        //         })
        //     })
        // },

        // 获取用户信息
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                axios.get('/tes/user/info').then(response => {
                    let res = response.data
                    if (res.code == 200) {
                        let roles = []
                        if (res.data.roleCode){
                            roles.push(res.data.roleCode)
                            commit('SET_ROLES', roles)
                        }
                        commit('SET_NAME', res.data.name)
                        commit('SET_AVATAR', res.data.iconPath)
                        resolve(response)
                    } else if (res.code == 401 || res.code == 404) {
                        commit('SET_TOKEN', '')
                        removeToken()
                        resolve()
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 登出
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    commit('SET_TOKEN', '')
                    commit('SET_ROLES', '')
                    removeToken()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                axios.get('/tes/logout').then(response => {
                    let res = response.data
                    if (res.code == 200) {
                        commit('SET_TOKEN', '')
                        removeToken()
                        resolve()
                        location.reload()
                    } else if (res.code == 401) {
                        commit('SET_TOKEN', '')
                        removeToken()
                        resolve()
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default user
